<?php
include_only();

        if( !ini_get('safe_mode') ){
            set_time_limit(240);
        } 
		else
		{
		echo "warning: safe mode is on cat't set time limit<br>";
		}
		
$user_info = get_user_info();

if(!empty($direct_upload) and $direct_upload=='yes'){
/*
echo "uploading file please wait <br>";

$user_info = get_user_info();
//print_r($user_info);

$target_path = "videos/";
$target_filename = getmicrotime().".flv" ;
$target_path = $target_path . $target_filename;
$original_file_name = basename($_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    mysql_query("INSERT INTO videos (userid, url, name)VALUES ('$user_info[id]', '$target_filename', '$original_file_name')");

	echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!<br>";
}

echo "redirecting please wait<br>";

echo "
<script type=\"text/javascript\">
<!--
window.location = \"index.php?page=my_videos\"
//-->
</script>

";
*/
}

if(!empty($youtube_upload) and $youtube_upload=='yes'){

$video_uploader = "";
//echo $youtube_video_link;
$video_id = explode("v=", $youtube_video_link);
$video_id = $video_id[1];
$video_id = explode("&", $video_id);
$video_id = $video_id[0];
//print_r($video_id);

    $url = "http://gdata.youtube.com/feeds/api/videos/". $video_id;
    $doc = new DOMDocument;
    $doc->load($url);
    $title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
	$description = $doc->getElementsByTagName("description")->item(0)->nodeValue;
	if(strlen($description)>255)$description = substr($description, 0,250)."...";
	$video_uploader = $video_uploader . "<b>Title:</b><br>" . $title;
	$video_uploader = $video_uploader . "<br><br><b>Description:</b><br>" . $description . "<br><br>";

	$image0 = "0" . generate_random_number('0000000000','9999999999') . getmicrotime() . ".jpg";
	$image1 = "1" . generate_random_number('0000000000','9999999999') . getmicrotime() . ".jpg";
	$image2 = "2" . generate_random_number('0000000000','9999999999') . getmicrotime() . ".jpg";
	$image3 = "3" . generate_random_number('0000000000','9999999999') . getmicrotime() . ".jpg";
	
if (!copy("http://img.youtube.com/vi/".$video_id."/0.jpg", "thumbnails/" . $image0)) echo "failed to copy $file...\n";
if (!copy("http://img.youtube.com/vi/".$video_id."/1.jpg", "thumbnails/" . $image1)) echo "failed to copy $file...\n";
if (!copy("http://img.youtube.com/vi/".$video_id."/2.jpg", "thumbnails/" . $image2)) echo "failed to copy $file...\n";
if (!copy("http://img.youtube.com/vi/".$video_id."/3.jpg", "thumbnails/" . $image3)) echo "failed to copy $file...\n";

$youtube_video_link = mysql_real_escape_string($youtube_video_link);
$title = mysql_real_escape_string($title);
$description = mysql_real_escape_string($description);
$current_time = getmicrotime();
//echo "INSERT INTO videos (userid, url, name, desc, image0, image1, image2, image3)VALUES ('$user_info[id]', '$url', '$title', '$description', '$image0', '$image1', '$image2', '$image3')";
mysql_query("INSERT INTO `videos` (`userid` ,`url` ,`name` ,`desc` ,`image0` ,`image1` ,`image2` ,`image3` ,`time`)VALUES ('$user_info[id]', '$youtube_video_link', '$title', '$description', '$image0', '$image1', '$image2', '$image3', '$current_time');");


$video_uploader = $video_uploader . "
<center>
<table>
	<tr>
		<td colspan='3'><center><img src='thumbnails/".$image0."'></center><br></td>
	</tr>
	<tr>
		<td><center><img src='thumbnails/".$image1."'></center></td>
		<td><center><img src='thumbnails/".$image2."'></center></td>
		<td><center><img src='thumbnails/".$image3."'></center></td>
	</tr>
</table>
</center>
<br>
";

}


if(!empty($remote_upload) and $remote_upload=='yes'){
//echo $remote_video_link;
//echo $remote_video_title; 
//echo $remote_video_desc; 
//120 * 90
//480 * 360
//echo $_SERVER["DOCUMENT_ROOT"].$_SERVER['PHP_SELF'];
//echo $_SERVER['PHP_SELF'];
//echo $_SERVER['SCRIPT_NAME'];
//echo $_SERVER["REQUEST_URI"] ;
//echo $_SERVER["SCRIPT_FILENAME"] ;
//echo dirname($_SERVER["SCRIPT_FILENAME"]);
//-itsoffset -4  -i test.avi -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 test.jpg
//echo read_option('ffmpeg_path');

$base_dir = dirname($_SERVER["SCRIPT_FILENAME"]);

	$image0 = "0" . generate_random_number('0000000000','9999999999') . getmicrotime() . ".jpg";
	$image1 = "1" . generate_random_number('0000000000','9999999999') . getmicrotime() . ".jpg";
	$image2 = "2" . generate_random_number('0000000000','9999999999') . getmicrotime() . ".jpg";
	$image3 = "3" . generate_random_number('0000000000','9999999999') . getmicrotime() . ".jpg";
	
	exec(read_option('ffmpeg_path').' -itsoffset -4  -i '.$remote_video_link.' -vcodec mjpeg -vframes 1 -an -ss 10 -f rawvideo -s 480x360 '.$base_dir.'/thumbnails/'.$image0);
	exec(read_option('ffmpeg_path').' -itsoffset -4  -i '.$remote_video_link.' -vcodec mjpeg -vframes 1 -an -ss 20 -f rawvideo -s 120x90 '.$base_dir.'/thumbnails/'.$image1);
	exec(read_option('ffmpeg_path').' -itsoffset -4  -i '.$remote_video_link.' -vcodec mjpeg -vframes 1 -an -ss 30 -f rawvideo -s 120x90 '.$base_dir.'/thumbnails/'.$image2);
	exec(read_option('ffmpeg_path').' -itsoffset -4  -i '.$remote_video_link.' -vcodec mjpeg -vframes 1 -an -ss 40 -f rawvideo -s 120x90 '.$base_dir.'/thumbnails/'.$image3);
	
$remote_video_link = mysql_real_escape_string($remote_video_link);
$remote_video_title = mysql_real_escape_string($remote_video_title);
$remote_video_desc = mysql_real_escape_string($remote_video_desc);
$current_time = getmicrotime();
//echo "INSERT INTO videos (userid, url, name, desc, image0, image1, image2, image3)VALUES ('$user_info[id]', '$url', '$title', '$description', '$image0', '$image1', '$image2', '$image3')";
mysql_query("INSERT INTO `videos` (`userid` ,`url` ,`name` ,`desc` ,`image0` ,`image1` ,`image2` ,`image3` ,`time`)VALUES ('$user_info[id]', '$remote_video_link', '$remote_video_title', '$remote_video_desc', '$image0', '$image1', '$image2', '$image3', '$current_time');");

	$video_uploader = '';
	$video_uploader = $video_uploader . "<b>Title:</b><br>" . $remote_video_title;
	$video_uploader = $video_uploader . "<br><br><b>Description:</b><br>" . $remote_video_desc . "<br><br>";	
	$video_uploader = $video_uploader . "
	<center>
	<table>
	<tr>
		<td colspan='3'><center><img src='thumbnails/".$image0."'></center><br></td>
	</tr>
	<tr>
		<td><center><img src='thumbnails/".$image1."'></center></td>
		<td><center><img src='thumbnails/".$image2."'></center></td>
		<td><center><img src='thumbnails/".$image3."'></center></td>
	</tr>
	</table>
	</center>
	<br>
";

}

?>

